﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mail;
using System.Web;
using DataLayer.Entities;
using DonutMgmtSystemFoundation.Models;
using Mvc.Mailer;
using ViewRes;

namespace DonutMgmtSystemFoundation.Helpers
{
    public class MailerHelper : MailerBase
    {
        public MailerHelper()
        {
            MasterName = "_Layout";
        }

        #region Login
        public virtual MvcMailMessage ForgetPassword(UserPassword userPassword, String link)
        {
            ViewBag.Link = link;
            ViewBag.Correo = System.Configuration.ConfigurationManager.AppSettings["Mail"];
            ViewData.Model = userPassword;
            return Populate(x =>
            {
                x.IsBodyHtml = true;
                x.Subject = LoginStrings.ForgotPassword;
                x.ViewName = "ForgetPassword_" + SessionContext.GetLanguage();
                x.To.Add(userPassword.User.Email);
            });
        }

        public virtual MvcMailMessage RegisterUser(User user, String link)
        {
            ViewBag.Link = link;
            ViewData.Model = user;
            return Populate(x =>
            {
                x.IsBodyHtml = true;
                x.Subject = LoginStrings.ActivateUser;
                x.ViewName = "RegisterUser_" + SessionContext.GetLanguage();
                x.To.Add(user.Email);
            });
        }

        public virtual MvcMailMessage AdminsRegisterUser(List<User> admins, User user, String link)
        {
            ViewBag.Link = link;
            ViewData.Model = user;
            MvcMailMessage message = Populate(x =>
            {
                x.IsBodyHtml = true;
                x.Subject = LoginStrings.MsgUserToActivate;
                x.ViewName = "AdminActivateUser_" + SessionContext.GetLanguage();
            });

            foreach (User admin in admins)
            {
                message.To.Add(admin.Email);
            }
            return message;
        }
        #endregion

        #region Admin

        public virtual MvcMailMessage SendOrder(MailModel model, List<UserDonutsModel> userList, DateTime date)
        {
            ViewBag.Date = date.ToString("dd-MMM-yyyy");
            int dozens = (userList.Count / 12);
            if (userList.Count % 12 > 0)
            {
                dozens++;
            }
            ViewBag.Dozens = dozens;
            ViewBag.Chosen = userList.Count;
            ViewBag.NotChosen = (dozens * 12) - userList.Count;
            ViewData.Model = userList;
            MvcMailMessage message = Populate(x =>
            {
                x.IsBodyHtml = true;
                x.Subject = model.Subject;
                x.ViewName = "AdminMailOrder";
            });

            AddEmailsToObject(message.To, model.To);
            AddEmailsToObject(message.Bcc, model.Bcc);
            return message;
        }

        public virtual MvcMailMessage SendPayment(MailModel model)
        {
            ViewBag.DozenPrice = ConfigurationManager.AppSettings["DozenPrice"];
            ViewBag.MoreThanOne = GetEmailsList(model.To).Count > 1;

            MvcMailMessage message = Populate(x =>
            {
                x.IsBodyHtml = false;
                x.Subject = model.Subject;
                x.ViewName = "SendPayment_" + SessionContext.GetLanguage();
            });
            AddEmailsToObject(message.To, model.To);
            AddEmailsToObject(message.Bcc, model.Bcc);

            return message;
        }

        public virtual MvcMailMessage SendDonutsArrived(MailModel model)
        {
            MvcMailMessage message = Populate(x =>
            {
                x.IsBodyHtml = false;
                x.Subject = model.Subject;
                x.ViewName = "SendDonutsArrived_" + SessionContext.GetLanguage();
            });

            AddEmailsToObject(message.To, model.To);
            AddEmailsToObject(message.Bcc, model.Bcc);
            return message;
        }

        #endregion

        private void AddEmailsToObject(IList list, string emails)
        {
            if (list != null && emails != null)
            {
                List<string> emailsList = GetEmailsList(emails);
                foreach (string email in emailsList)
                {
                    list.Add(new MailAddress(email));
                }
            }
        }

        private List<string> GetEmailsList(string emails)
        {
            List<string> list = new List<string>();
            foreach (string email in emails.Split(';').ToList())
            {
                if (email != null && !string.IsNullOrEmpty(email.Trim()))
                {
                    list.Add(email);
                }
            }
            return list;
        }
    }
}